/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschool;

import java.util.List;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 *
 * @author tommy & Elena Medvedeva
 */
@Stateless
@Local(value=StudentServiceLocal.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
public class StudentService implements StudentServiceLocal{

    @Resource
    private SessionContext ctx;
    
    @EJB
    private StudentDaoLocal studentDao;

    public void setStudentDao(StudentDaoLocal studentDao) {
        this.studentDao = studentDao;
    }
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public Student add(Student student) {
        if (student == null) {
            throw new IllegalArgumentException("Method StudentService.add call with Null argument student");
        }
        studentDao.add(student);
        if (false) {
            ctx.setRollbackOnly();
            return null;
        }
        return student;

    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Student get(Long id) {
        if(id == null){
          throw new IllegalArgumentException("Method StudentDao.get call with Null argument id");           
        }
        return studentDao.get(id);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Student> getAll() {        
        return studentDao.getAll();
    }
    
}
